<?php

namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;
use PHPExcel;
use PHPExcel_Writer_Excel5;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Fill;
use PHPExcel_Style_Border;

class Rcpay extends Common
{
	//支付
	public function cost1($page=1,$list=10){
		$request = Request::instance();$data = $request->post();


		$where="ap.is_deleted=0 and ap.is_actived=1 and ap.state=7 and (ap.pizhunjin>ap.zhifujin or ap.zhifujin is null) ";
		if($data){
			if(trim($data["no"])){
				$no=trim($data["no"]);
				$where.="and ap.no='$no' ";
			}
			
			if(trim($data["start"])){
				$start=strtotime(trim($data["start"]));
				$where.="and ap.ctime>=$start ";
			}
			if(trim($data["end"])){
				$end=strtotime(trim($data["end"]));
				$where.="and ap.ctime<=$end ";
			}
			
			if(trim($data["shen"])){
				$data["shen"]=trim($data["shen"]);
				$where.="and ce.em_name='{$data["shen"]}' ";
				
			}
			if(trim($data["shou"])){
				$data["shou"]=trim($data["shou"]);
				$where.="and eb.em_name='{$data["shou"]}' ";
			}
			
		}else{
			$data=["no"=>"","start"=>"","end"=>"","shen"=>"","shou"=>""];
		}
		$count=Db::table("rc_baoxiao_apply")->alias("ap")->where("ap.is_deleted=0 and ap.is_actived=1 and ap.state=7 and (ap.pizhunjin>ap.zhifujin or ap.zhifujin is null)")->count();
		$jine=Db::table("rc_baoxiao_apply")->alias("ap")->where("ap.is_deleted=0 and ap.is_actived=1 and ap.state=7 and (ap.pizhunjin>ap.zhifujin or ap.zhifujin is null)")->sum("ap.pizhunjin-ap.zhifujin");
		$this->assign("count",$count);
		$this->assign("jine",$jine);
		$sum=Db::table("rc_baoxiao_apply")->alias("ap")
		->join("employee ce","ap.cuser=ce.id")
		->join("employee eb","ap.baoxiaoren=eb.id")
		->where($where)->count();
		 $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

		$apply=Db::table("rc_baoxiao_apply")->alias("ap")
		->join("employee ce","ap.cuser=ce.id")
		->join("employee eb","ap.baoxiaoren=eb.id")
		->where($where)->order("ap.id")->limit($start,$list)
		->field("ap.id,ap.no,ap.title,ap.state,ce.em_name as cman,eb.em_name as bman,ap.pizhunjin,ap.zhifujin,ap.ctime")
		->select();
		for($i=0;$i<count($apply);$i++){
			$apply[$i]["ctime"]=date("Y-m-d",$apply[$i]["ctime"]);
		}
		$this->assign("apply",$apply);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);

		return $this->fetch();
	}

	//支付凭证
	public function zhifu($aid){
		$this->assign("aid",$aid);
		$apply=Db::table("rc_baoxiao_apply")->where(["id"=>$aid])->find();
		//print_r($apply);
		$apply["cman"]=Db::table("employee")->where(["id"=>$apply["cuser"]])->value("em_name");
		$apply["ctime"]=date("Y-m-d",$apply["ctime"]);
		$tmp=Db::table("cg_kaoheqi2")->alias("k2")
		->join("cg_kaoheqi1 k1","k2.pid=k1.id")
		->join("cg_cainian c","k1.cid=c.id")
		->where("k2.id={$apply["kaohe"]}")
		->field("c.title as nian,k2.title as yue")->find();

		$apply["kaohe"]=$tmp["nian"].'-'.$tmp["yue"];
		$apply["payway"]=Db::table("cg_paymethod")->where(["id"=>$apply["payway"]])->value("title");
		$apply["bman"]=Db::table("employee")->where(["id"=>$apply["baoxiaoren"]])->value("em_name");
		$apply["jrbm"]=Db::table("cg_fijiruzd")->alias("fj")->join("department d", "fj.title=d.id")->where("fj.id={$apply["jrbm"]}")->value("d.de_name");
		$apply["weifujin"]=$apply["pizhunjin"]-$apply["zhifujin"];
		$feiyong=Db::table("cg_feiyongkemu3")->where("id in ({$apply["feis"]})")->select();
		$this->assign("feiyong",$feiyong);
		$feiyongnum=count($feiyong);
		$this->assign("feiyongnum",$feiyongnum);
		if($apply["chuchai"]){
			
			$apply["chuchai"]=Db::table("chuchai_apply")->where("id in ({$apply["chuchai"]})")->select();
		}
		if($apply["wanneng"]){
			
			$apply["wanneng"]=Db::table("wanneng_apply")->where("id in ({$apply["wanneng"]})")->select();
		}
		$this->assign("apply",$apply);
		$applyde=Db::table("rc_baoxiao_detail")->alias("apd")
		->join("cg_jxs j","apd.jxs=j.id","LEFT")
		->join("employee e","apd.shenpiren=e.id","LEFT")
		->where(["aid"=>$aid])
		->field("apd.start,apd.end,apd.chufa,apd.mudi,apd.feiid,apd.feis,apd.pizhunfeiyong,apd.remark,j.title as jxs,e.em_name,apd.state")
		->select();
		
		$this->assign("applyde",$applyde);
		$fujian=[];
			//print_r($apply);
			if($apply["fujian"]){
				$fujian=Db::table("annex")->where("id in ({$apply["fujian"]})")->select();
				//print_r($fujian);
			}
		$this->assign("fujian",$fujian);


		///支付
		$zhifu=Db::table("rc_baoxiao_pay")->alias("ap")
		->join("employee c","ap.cuser=c.id")
		->join("employee p","ap.zfuser=p.id")
		->where(["ap.aid"=>$aid])
		->field("c.em_name as cu,p.em_name as pu,ap.ctime,ap.zftime,ap.pzno,ap.jine,ap.remark")
		->select();
		for($i=0;$i<count($zhifu);$i++){
			$zhifu[$i]["ctime"]=date("Y-m-d",$zhifu[$i]["ctime"]);
			$zhifu[$i]["zftime"]=date("Y-m-d",$zhifu[$i]["zftime"]);
		}
		$this->assign("zhifu",$zhifu);
		return $this->fetch();
	}
	//支付处理
	public function zhifuchuli(){
		$request = Request::instance();$data = $request->post();
		//print_r($data);exit;
		if($data){
			if($data["aid"])
				$adddata["aid"]=$data["aid"];
			else
				return json_encode(["state"=>2,"msg"=>"请正确提交操作"]);
			if(trim($data["pzno"]))
				$adddata["pzno"]=trim($data["pzno"]);
			else
				return json_encode(["state"=>2,"msg"=>"支付凭证不能为空"]);
			if(trim($data["zfuser"]))
				$adddata["zfuser"]=trim($data["zfuser"]);
			else
				return json_encode(["state"=>2,"msg"=>"支付人不能为空"]);
			if(trim($data["zftime"]))
				$adddata["zftime"]=strtotime(trim($data["zftime"]));
			else
				return json_encode(["state"=>2,"msg"=>"支付时间不能为空"]);
			if(trim($data["jine"])){
				try{
					$adddata["jine"]=(float)trim($data["jine"]);
				}catch (\Exception $e){
					
					return json_encode(["msg"=>"请正确填写金额","state"=>2]);
				}
				
			}else
				return json_encode(["state"=>2,"msg"=>"支付金额不能为空"]);
			if(trim($data["remark"]))
				$adddata["remark"]=trim($data["remark"]);
			$adddata["ctime"]=time();
			$adddata["cuser"]=Session::get("employee_id");
			
			$quekou=Db::table("rc_baoxiao_apply")->where(["id"=>$data["aid"]])->sum("pizhunjin-zhifujin");
			if($quekou<$adddata["jine"]){
				return json_encode(["state"=>2,"msg"=>"支付金额不能超出未支付部分"]);
			}
			Db::startTrans();
			try{
				$res1=Db::table("rc_baoxiao_pay")->insert($adddata);
				$jine=Db::table("rc_baoxiao_pay")->where(["aid"=>$data["aid"]])->sum("jine");
				$res2=Db::table("rc_baoxiao_apply")->where(["id"=>$data["aid"]])->update(["zhifujin"=>$jine,"iszhifu"=>1]);
			}catch (\Exception $e){
				Db::rollBack();	

				//return json_encode(["msg"=>$e->getMessage(),"state"=>2]);
				return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
			}
			if($res1&&$res2){
				Db::commit();
				return json_encode(["msg"=>"","state"=>1]);
			}else{
				Db::rollBack();	
				return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
			}
		}else{
			return json_encode(["state"=>2,"msg"=>"请正确提交操作"]);
		}
	}
	//已支付
	public function cost2($page=1,$list=10){
		$request = Request::instance();$data = $request->post();
		$where="1=1 ";
		if($data){
			if(trim($data["no"])){
				$no=trim($data["no"]);
				$where.="and ap.no='$no' ";
			}
			
			if(trim($data["start"])){
				$start=strtotime(trim($data["start"]));
				$where.="and p.zftime>=$start ";
			}
			if(trim($data["end"])){
				$end=strtotime(trim($data["end"]));
				$where.="and p.zftime<=$end ";
			}
			
			if(trim($data["shen"])){
				$data["shen"]=trim($data["shen"]);
				$where.="and ce.em_name='{$data["shen"]}' ";
				
			}
			if(trim($data["shou"])){
				$data["shou"]=trim($data["shou"]);
				$where.="and eb.em_name='{$data["shou"]}' ";
			}
			if(trim($data["pzno"])){
				$pzno=trim($data["pzno"]);
				$where.="and p.pzno='$pzno' ";
			}
		}else{
			$data=["no"=>"","start"=>"","end"=>"","shen"=>"","shou"=>"","pzno"=>""];
		}
		$count=Db::table("rc_baoxiao_pay")->count();
		$jine=Db::table("rc_baoxiao_pay")->sum("jine");
		$this->assign("count",$count);
		$this->assign("jine",$jine);
		
		$sum=Db::table("rc_baoxiao_pay")->alias("p")
		->join("rc_baoxiao_apply ap","p.aid=ap.id")
		->join("employee ce","ap.cuser=ce.id")
		->join("employee eb","ap.baoxiaoren=eb.id")
		->where($where)->count();
		 $maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
		$apply=Db::table("rc_baoxiao_pay")->alias("p")
		->join("rc_baoxiao_apply ap","p.aid=ap.id")
		->join("employee ce","ap.cuser=ce.id")
		->join("employee eb","ap.baoxiaoren=eb.id")
		->where($where)->order("p.zftime desc")->limit($start,$list)
		->field("ap.id,ap.no,ap.title,ap.state,ce.em_name as cman,eb.em_name as bman,p.jine,p.pzno,p.zftime")
		->select();
		for($i=0;$i<count($apply);$i++){
			$apply[$i]["zftime"]=date("Y-m-d",$apply[$i]["zftime"]);
		}
		$this->assign("apply",$apply);
        $this->assign("sum",$sum);
        $this->assign("page",$page);
        $this->assign("list",$list);
        $this->assign("maxpage",$maxpage);
        $this->assign("data",$data);
		return $this->fetch();
	}
}